<?php if(!defined('BASEPATH')) exit ('No direct script access allowed');

/**---------------------------
* Giahuy
* @category		Home controller extends Public_Controller
* @Author		Nguyen Duc Hung - tinapc@gmail.com
* @Date			6/8/2012
* @Version		v1.0
-----------------------------*/


class Home extends public_controller
{

    function __construct()
    {
        parent::__construct();
		$this->load->library('fb_api/facebook', array("appId" => $this->config->item('appId') , "secret" => $this->config->item('secret')));
       if(!$this->session->userdata('loginStatus')) redirect('landing');
    }

    function index()
    {
        $this->db->order_by('id', 'desc');
        $this->db->limit(12);
    	$infoPerson = $this->db->select('id, name, photo_cover, country, state, age')->get_where('person', array('public' => 1))->result_array();
    	$this->template->title('Death project')
            ->set('peoples', $infoPerson)
    		->build('default/index');
    }

    public function search() {
        $key = $this->input->get('key');
        $this->db->like('name', $key);
        $sql = $this->db->select('id, name, photo_cover, state, country, age')->get('person')->result_array();
        
        $this->template->title('Result search', 'Death project')
            ->set('results', $sql)
            ->build('default/search');
    }

    public function load_more(){
        $start = (int)$this->input->post('start');
        $this->db->order_by('id', 'desc');
        $this->db->limit(12, $start);
        $sql = $this->db->select('id, name, photo_cover, country, state, age')->get_where('person', array('public' => 1))->result_array();

        if(count($sql)) {
            foreach($sql as $row) {
                if($row['photo_cover'] != '') {
                    $avatar = '<img src="'. base_url() . $row['photo_cover'] .'" alt="" class="img-polaroid">';
                } else {
                    $avatar = '<img src="'. base_url() .'public/image/NoImage.jpg" alt="" class="img-polaroid">';
                }

                $url = site_url('person/' . $row['id'].'-' . $this->util->alias($row['name']));

                echo '<div class="col-lg-3 item-person-in-list">';  
                echo '<div class="item-add-person">';
                echo $avatar;
                echo '</div>';
                echo '<div class="caption">';
                echo '<h4><a href="'.$url.'">'.$row['name'].'</h4></a><p>';
                echo $this->person_model->get_name_state($row['state']).', ';
                echo $this->person_model->get_name_country($row['country']).', ';           
                echo $row['age'];
                echo '<p>';
                echo '</div>';
                echo '</div>';
                echo '</div>';
            }
        } else {
            //echo "<p>No image from now</p>";
        }
    }

}
/* End of file home.php */
/* Local: ./application/controllers/home.php */